/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Dictionary;

import com.example.SinhalaForOO;
import com.sun.star.uno.XComponentContext;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * This class provides methods to show a meaning of a selected word from Open Office.
 *
 * @author SUMEDHA
 */
public class FrameDisplay {
    private XComponentContext m_xContext;
    private DataBaseReader dataBaseReader;

    /**
     * 
     * @param m_xContext To Access Objects of Open Office
     * @param dataBaseReader  To read the dictionary databases
     */
    public FrameDisplay(XComponentContext m_xContext,DataBaseReader dataBaseReader) {
        this.m_xContext=m_xContext;
        this.dataBaseReader=dataBaseReader;
    }

    /**
     * This method shows the meaning of the selected word.
     * It reads the selected word from Open Office and identifies the language
     * of the word and accordingly shows the meaning.
     * @throws UnsupportedEncodingException
     * @throws FileNotFoundException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void showMeaning() throws UnsupportedEncodingException,
            FileNotFoundException, IOException, ClassNotFoundException {
        AutomaticDictionary automaticDictionary = new AutomaticDictionary(m_xContext);
        String meaning;
        //Reading the selected word
        String word = automaticDictionary.getSelectedText();
        /*
         * Getting the integer value of the first character of the selected word
         * to identify the languge. i.e. Use the unicode values to identify the language
         */
        int type = word.codePointAt(0);

        //If the word is in english
        if (type > 64 && type < 123) {
            //Converting to lower case
            word = word.toLowerCase();
            //Geting the menaing from the loaded dictionary data
            meaning = dataBaseReader.getEngToSin().get(word);
            displayMeaning(meaning, word);
        } //Else if the word is in sinhala
        else if (type > 3455 && type < 3553) {
            //Geting the menaing from the loaded dictionary data
            meaning = dataBaseReader.getSinToEng().get(word);
            displayMeaning(meaning, word);
        }

    }

    /**
     * The method to display the meaning in a Frame, meaning is passed form the
     * showMeaning() method above.
     * @param meaning meaning of the word
     * @param word   word which need the meaning
     */
    public void displayMeaning(String meaning, String word) {

        //Cheking whether the word is in the dictionary
        if (meaning != null) {
            Frame fr = new Frame(meaning, word);
            fr.setVisible(true);

        }//If the word is not in the dictionary
        else {
            try {
                //Setting the System look and feel
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                //If the word is not in the Dictionary
                JOptionPane.showMessageDialog(null, "Sorry. The word is not "
                        + "in the database. Try a new Word", "Message", 1, null);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(SinhalaForOO.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}
